/*
 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#define private public
#define protected public
#include "suffix_match_trie.h"

using namespace std;
using namespace testing::ext;
using namespace OHOS::NetManagerStandard;

class SuffixMatchTrieTest : public testing::Test {
public:
    static void SetUpTestCase();
    static void TearDownTestCase();
    void SetUp();
    void TearDown();
};

void SuffixMatchTrieTest::SetUpTestCase() {}

void SuffixMatchTrieTest::TearDownTestCase() {}

void SuffixMatchTrieTest::SetUp() {}

void SuffixMatchTrieTest::TearDown() {}

HWTEST_F(SuffixMatchTrieTest, SuffixMatchTrieTest001, TestSize.Level0)
{
    SuffixMatchTrie<int> trie;
    EXPECT_TRUE(trie.Empty());

    const char *ducom = "du.com";
    trie.Insert(ducom, 101);

    EXPECT_FALSE(trie.Empty());

    int val = 0;
    int len = trie.LongestSuffixMatch("adu.com", val);

    EXPECT_EQ(len, strlen(ducom));
    EXPECT_EQ(val, 101);
}

HWTEST_F(SuffixMatchTrieTest, SuffixMatchTrieTest002, TestSize.Level0)
{
    SuffixMatchTrie<int> trie;
    const char *ducom = "du.com";
    trie.Insert(ducom, 101);
    int val = 0;
    int len = trie.LongestSuffixMatch("u.com", val);

    EXPECT_EQ(len, 0);
    EXPECT_EQ(val, 0);
}

HWTEST_F(SuffixMatchTrieTest, SuffixMatchTrieTest003, TestSize.Level0)
{
    SuffixMatchTrie<int> trie;
    const char *ducom = "du.com";
    const char *baiducom = "baidu.com";
    trie.Insert(ducom, 101);
    trie.Insert(baiducom, 102);
    int val = 0;
    int len = trie.LongestSuffixMatch("tieba.baidu.com", val);

    EXPECT_EQ(len, strlen(baiducom));
    EXPECT_EQ(val, 102);
}

HWTEST_F(SuffixMatchTrieTest, SuffixMatchTrieTest004, TestSize.Level0)
{
    SuffixMatchTrie<int> trie;
    const char *ducom = "du.com";
    const char *baiducom = "baidu.com";
    trie.Insert(ducom, 101);
    trie.Insert(baiducom, 102);
    trie.Insert("csdn.com", 200);
    trie.Update("du.com", 201);

    int val = 0;
    int len = trie.LongestSuffixMatch("adu.com", val);

    EXPECT_EQ(len, strlen(ducom));
    EXPECT_EQ(val, 201);
}